package com.myjavalab.dotsandboxes;

public class Board {

	private char[][] model;
	private int[] coordinate;
	
	private int dotNum;     //the number of dot
	private int width;      //the width of model
	private int boxNum;     //the number of box
	private int remainBoxNum;      //
	private char currentPlayer;   //sign of user
	private int userScore;     //user's score
	private int computerScore;   //computer's score
	
	public static final char USER_SIGN = 'U';
	public static final char COMPUTER_SIGN = 'C';
	public static final char BLANK = ' ';
	public static final char DASH = '-';
	public static final char VERTICAL_BAR = '|';
	
	public Board(int dotNum) {
		this.dotNum = dotNum;
		this.width = dotNum * 2 - 1;
		this.boxNum = (dotNum - 1) * (dotNum -1);
		this.remainBoxNum = boxNum;
		this.model = new char[width][width];
		this.coordinate = new int[width];
		this.currentPlayer = USER_SIGN;
		init();
	}
	
	public void init(){
		for(int i = 0 ; i < width; i++){
			coordinate[i] = i;
		}
		for(int i = 0; i < width; i++) {
			if(i%2 == 0){
				for(int j = 0; j < width; j++) {
					if(j%2 == 0) 
						model[i][j] = 'o';
					else
						model[i][j] = BLANK;
				}				
			}else {
				for(int j = 0; j < width; j++) {
					model[i][j] = BLANK;					
				}
			}
		}
		print();
	}
	
	/**
	 * print out board model
	 */
	public void print(){
		System.out.print("  ");
		for(int i = 0 ; i < width; i++){
			System.out.print(coordinate[i]);
		}
		System.out.println();
		for(int i = 0; i < width; i++) {
			System.out.print(coordinate[i]);
			System.out.print(" ");
			for(int j = 0; j < width; j++) {
				System.out.print(model[i][j]);
			}
			System.out.println();
		}
		System.out.println("--------------");
	}
	
	public char[][] getModel(){
		return model;
	}
	

	private void refresh(){
		int rest = this.remainBoxNum;
		for(int i = 1; i < width; i = i+2){
			for(int j = 1; j < width; j = j+2){
				if(model[i][j] == BLANK && model[i-1][j] != BLANK && model[i+1][j] != BLANK && model[i][j-1] != BLANK && model[i][j+1] != BLANK ){
					model[i][j] = this.currentPlayer;
					
					this.remainBoxNum--;
					if(this.currentPlayer == USER_SIGN) 
						this.userScore++;
					else 
						this.computerScore++;
					break;
				}
			}
			if(rest != this.remainBoxNum){
				break;
			}
		}
		if(rest == this.remainBoxNum) {
			if(this.currentPlayer == USER_SIGN) 
				this.currentPlayer = COMPUTER_SIGN;   
			else 
				this.currentPlayer = USER_SIGN;
		}
		print();
	}
	
	/**
	 * update board model
	 */
	public void update(int row, int column){
		if(model[row][column] != BLANK) {
			System.out.println("Wrong number entered");			
			return;
		}
		if(row%2 == 0)
			model[row][column] = DASH;
		else 
			model[row][column] = VERTICAL_BAR;	
		refresh();
	}
	
	/**
	 * to check if the game is over
	 * @return
	 */
	public boolean isGameEnd(){
		if(remainBoxNum == 0) 
			return true;
		return false;
	}
	
	public int getDotNum() {
		return dotNum;
	}

	public char getCurrentPlayer() {
		return currentPlayer;
	}

	public void setCurrentPlayer(char currentPlayer) {
		this.currentPlayer = currentPlayer;
	}

	public int getBoxNum() {
		return boxNum;
	}

	public int getWidth() {
		return width;
	}

	public int getRemainBoxNum() {
		return remainBoxNum;
	}

	public int getUserScore() {
		return userScore;
	}

	public int getComputerScore() {
		return computerScore;
	}

	
}
